﻿using System;
using System.Linq;
using System.Web.Mvc;
using Matrix.MaKaYunShang.BLL;
using Matrix.MaKaYunShang.BLL.BllExt;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Enum;

namespace Matrix.MaKaYunShang.MagWeb.Controllers
{
    [Authorization]
    [Matrix.MaKaYunShang.Model.Attributes.Skip]
    public class ConsumerController : BaseController
    {
         #region 列表
        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        [Matrix.MaKaYunShang.Model.Attributes.Skip]
        public JsonResult Listing(int draw, int start, int length)
        {
            var search = Request["search[value]"];
            var pageIndex = (start / length) + 1;
            var shopIds = string.Empty;
            if (!string.IsNullOrEmpty(search))
            {
                var shopList = ShopsBLL.Instance.GetListByName(search);
                if (shopList.Count > 0)
                {
                    shopIds = string.Join(",", shopList.Select(l => l.Id));
                }
                else
                {
                    shopIds = "-1";
                }
            }
            var theList = ConsumerBLL.Instance.GetPagingList(MagModules, draw, pageIndex, length, SqlSafe(search), shopIds, true);
            var jsonResult = Json(theList);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }
        #endregion

        /// <summary>
        /// 更改绑定店铺
        /// </summary>
        /// <param name="consumerId">用户ID</param>
        /// <param name="shopId">需绑定的店铺ID（为0则为解绑）</param>
        /// <param name="isSetDefaultShop">是否为设置默认访问店铺操作（0否  1是）</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult ChangeShop(int consumerId, int shopId, int isSetDefaultShop = 0)
        {
            var result = new Result();
            var consumer = ConsumerBLL.Instance.GetModel(consumerId);
            if (consumer != null)
            {
                if (isSetDefaultShop == 0)
                {
                    var nOldShopId = consumer.ShopId;
                    if (shopId == 0) //解绑
                    {
                        consumer.ShopId = 0;
                        consumer.ShopAccountId = 0;
                        result.Status = ConsumerBLL.Instance.Update(consumer);
                        if (result.Status)
                        {
                            //Added by leo 2016-09-17
                            //TODO: 临时写死商品ID参数等， 
                            var orderExtBll = new OrderExtBll();
                            orderExtBll.DealEWaterCouponAfterConsumerUnbinding(consumerId, nOldShopId, 87, 2, 3);
                        }
                        return Json(result);
                    }
                    var lastShopId = consumer.ShopId;
                    var lastMcAccountId = consumer.ShopAccountId;
                    var mcAccount =
                        McAccountsBLL.Instance.GetModelList("ShopId=" + shopId + " and AccountType=1").FirstOrDefault();
                    if (mcAccount != null)
                    {
                        consumer.ShopId = shopId;
                        consumer.ShopAccountId = mcAccount.Id;
                        result.Status = ConsumerBLL.Instance.Update(consumer);
                        if (result.Status)
                        {
                            //添加更改店铺记录
                            ChangeShopRecordBLL.Instance.Add(new ChangeShopRecord
                            {
                                ConsumerId = consumerId,
                                LastShopId = lastShopId,
                                LastMcAccountId = lastMcAccountId,
                                CurrentShopId = shopId,
                                CurrentMcAccountId = mcAccount.Id,
                                OperaterId = CurrentUser.Id,
                                AddTime = DateTime.Now
                            });

                            //Added by leo 2016-09-17
                            //TODO: 临时写死商品ID参数等， 
                            var orderExtBll = new OrderExtBll();
                            orderExtBll.DealEWaterCouponAfterConsumerUnbinding(consumerId, nOldShopId, 87, 2, 3);
                        }
                    }
                }
                else
                {
                    var newConsumerShopId = ConsumerShopBLL.Instance.Add(new ConsumerShop
                    {
                        ConsumerId = consumerId,
                        ShopId = shopId,
                        RecordType = (int)CommonEnum.ConsumerShopRecordType.Artificial
                    });
                    result.Status = newConsumerShopId > 0;
                }
            }
            if (!result.Status && string.IsNullOrEmpty(result.Message))
            {
                result.Message = "数据有误，请重试";
            }
            return Json(result);
        }
    }
}